package com.example.treeHole.service.impl;

import com.example.treeHole.dao.ArticleMainMapper;
import com.example.treeHole.dao.NotificationMainMapper;
import com.example.treeHole.dao.UserInfoMapper;
import com.example.treeHole.dao.VirtualCharacterMapper;
import com.example.treeHole.service.NotificationMainService;
import com.example.treeHole.utils.BaseContext;
import com.example.treeHole.vo.ArticleMain;
import com.example.treeHole.vo.NotificationMain;
import com.example.treeHole.vo.UserInfo;
import com.example.treeHole.vo.VirtualCharacter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NotificationMainServiceImpl implements NotificationMainService {

    @Autowired
    private NotificationMainMapper notificationMainMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private ArticleMainMapper articleMainMapper;

    @Autowired
    private VirtualCharacterMapper virtualCharacterMapper;

    @Override
    public List<NotificationMain> findByType(String type) {
        //List<NotificationMain> list=notificationMainMapper.findByType(type);
        return null;
    }

    /**
     * 查询系统消息
     * @param type
     * @return
     */
    public List<NotificationMain> findSysByType(String type) {
        String id = BaseContext.getCurrentId();
        //查询是否为管理员
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(id);
        List<NotificationMain> adminNs=new ArrayList<>();
        if(userInfo.getRoleType()>2){
            //查询系统消息
            List<NotificationMain> adminNs1 = notificationMainMapper.findAdminSysByType(id);
            adminNs.addAll(adminNs1);
        }
        //公共消息：点赞收藏
        List<NotificationMain> mains=notificationMainMapper.findSysByType(id);
        mains.addAll(adminNs);
        if(mains.size()>0){
            List<NotificationMain> sortNoMessage = mains.stream()
                    .sorted(Comparator.comparing(NotificationMain::getCreateDate)
                            .reversed())
                    .collect(Collectors.toList());
            return sortNoMessage;
        }
        return mains;
    }

    @Override
    public void approve(String id) {
        ArticleMain articleMain = new ArticleMain();
        articleMain.setArticleId(id);
        articleMain.setStatus("1");
        articleMainMapper.updateByPrimaryKeySelective(articleMain);
        notificationMainMapper.updateByArticleAndNC(id);
    }

    @Override
    public void approve1(String id) {
        VirtualCharacter virtualCharacter = new VirtualCharacter();
        virtualCharacter.setId(id);
        virtualCharacter.setVirtuallyStatus("1");
        virtualCharacterMapper.updateByPrimaryKeySelective(virtualCharacter);
        notificationMainMapper.updateByArticleAndNC(id);
    }
}
















